package qualomelhor.server;

import java.io.IOException;
import javax.servlet.http.*; 
import qualomelhor.database.Usuario;
import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.Parameter;
import com.restfb.types.FacebookType;
import com.restfb.types.User;

public class FeedFBServlet extends HttpServlet { 

	private static final long serialVersionUID = 0;

	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		doPost(req, resp);
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { 

		String app_id = "";	
		String app_secret = "";
		String namespace = "";
		if (req.getServerName().equals("www.qual-o-melhor.com")) {
			app_id = "343538035658997";
			app_secret = "8023bbeef61df4d7d0d059c4e9440427";
			namespace = "qual-o-melhor";
		} else if (req.getServerName().equals("www.which-is-better.net")) {
			app_id = "251470321613608"; 
			app_secret = "602baa2419a4306a1ab3d824201f9d82";
			namespace = "which-is-the-better";
		} else if (req.getServerName().equals("www.witch-is-better.com")) {
			app_id = "251471681589048";
			app_secret = "4367064af22143b7a397751083c7edaf";
			namespace = "witch-is-better.com";
		} else if (req.getServerName().equals("localhost")) {
			app_id = "251470321613608"; 
			app_secret = "602baa2419a4306a1ab3d824201f9d82";
			namespace = "which-is-the-better";
		}

		resp.setContentType("Text/Plain");

		HttpSession sess = req.getSession(true);
		Usuario u = null;
		if (sess.getAttribute("UsuarioAtual") != null) {
			u = (Usuario)sess.getAttribute("UsuarioAtual");
			System.out.println("accessToken = " + u.getAccessToken());

			String acao = req.getParameter("acao");

			if (acao.equals("feed")) {
				sendFeedFB(u.getAccessToken());
			} else if (acao.equals("action")) {
				sendActionFeedFB(u.getAccessToken(), namespace, "compare", "item", "http://" + req.getServerName() + "/fbobject");
			}

			resp.getWriter().print("mensagem postada!");

		} else {
			System.out.println("Usuario nao logado!!");
		}
	}

	private User getUserInfo(String accessToken) {
		FacebookClient facebookClient = new DefaultFacebookClient(accessToken);
		User user = facebookClient.fetchObject("me", User.class);
		System.out.println("user name = " + user.getName());
		System.out.println("email = " + user.getEmail());
		System.out.println("locale = " + user.getLocale());
		System.out.println("user id = " + user.getId());
		return user;
	}
	
	private void sendFeedFB(String accessToken) {
		FacebookClient facebookClient = new DefaultFacebookClient(accessToken);
		FacebookType publishMessageResponse =
			  facebookClient.publish("me/feed", FacebookType.class,
			    Parameter.with("message", "Testando...."));

			System.out.println("Published message ID: " + publishMessageResponse.getId());
	}
	
	private void sendActionFeedFB(String accessToken, String namespace, String action, String object, String link) {
		FacebookClient facebookClient = new DefaultFacebookClient(accessToken);
		FacebookType publishMessageResponse =
			  facebookClient.publish("me/" + namespace + ":" + action, FacebookType.class,
			    Parameter.with(object, link));

			System.out.println("Published message ID: " + publishMessageResponse.getId());

	/*
		FB.api('/me/<%=namespace%>:Compare?Item=http://<%=request.getServerName()%>/fbobject',
			'post',
			function(response) {
   				if (!response || response.error) {
      				alert('Error occured');
   				} else {
      				alert('Cook was successful! Action ID: ' + response.id);
   				}
		});
	*/
	}

	private void imprimeUserInfo(HttpServletResponse resp, User u) {
		try {
			resp.getWriter().println(u.getName());
			resp.getWriter().println("id = " + u.getId());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}